<?php
/**
 * 菜单管理
 */
namespace app\admins\controller\sys;
use \app\admins\controller\Auth;
use \think\Exception;
class Menu extends Auth{
    
    
    public function index(){
        $plat = input('plat',2);
        $platlist = config('servces.site_plat_list');        
        $Menu = new \logic\sys\power\Menu($this->getUser());
        $menulist = $Menu->getList(['plat'=>$plat,'status'=>1,'searchkey'=>$searchkey]);
        $menulist['list'] = nodeMerge($menulist['list']);
        $this->assign(get_defined_vars());
        return $this->fetch();
    }
    
    /**
     * 添加顶级节点或编辑接口
     */
    public function addmenu($plat , $pid=0){
        $Menu = new \logic\sys\power\Menu($this->getUser());
        if($pid==0){
            $menuinfo['menuname'] = '顶级节点';
            $menuinfo['depth'] = 0;
        }else{
            $menuinfo = $Menu->find(['id'=>$pid]);
        }
        if(request()->isPost()){
            try{
                $param = array(
                    'id'        => input('id'),
                    'pid'       => input('pid'),
                    'menuname'  => input('menuname'),
                    'icon'      => input('icon'),
                    'remark'    => input('remark'),
                    'sort'      => input('sort'),
                    'status'    => 1,
                    'depth'     => $menuinfo['depth']+1,
                    'plat'      => input('plat')
                );
                $result = $Menu->add($param);
                return json(array('code'=>1,'msg'=>'添加成功'));
            }catch(Exception $e){
                return json(array('code'=>0,'msg'=>$e->getMessage()));
            }
        }
        $menulist = $Menu->getList(['plat'=>$plat,'status'=> 3 ]);
        $this->assign(get_defined_vars());
        return $this->fetch();
    }
    
    /**
     * 修改节点
     */
    public function editmenu($id,$plat){
        $Menu = new \logic\sys\power\Menu($this->getUser());
        $menuinfo = $Menu->find(['id'=>$id]);
        if(request()->isPost()){
            try{
                $param = array(
                    'pid'       => input('pid'),
                    'menuname'  => input('menuname'),
                    'icon'      => input('icon'),
                    'remark'    => input('remark'),
                    'sort'      => input('sort')
                );
                $result = $Menu->edit($id,$param);
                if( $result->notpower ) E($result->msg);
                return json(array('code'=>1,'msg'=>'修改成功'));
            }catch(Exception $e){
                return json(array('code'=>0,'msg'=>$e->getMessage()));
            }
        }
        $menulist = $Menu->getList(['plat'=>$plat,'status'=>1,'depth'=>$menuinfo['depth']-1]);
        //$Code = new \logic\sys\power\Code($this->getUser());
        //$codelist = $Code->setCallUser($this->getUser(true))->getList();
        $this->assign(get_defined_vars());
        return $this->fetch();
    }
    /**
     *删除节点
     */
    public function delmenu($id){
        try{
            $Menu = new \logic\sys\power\Menu($this->getUser());
            $result = $Menu->del($id);
            return json(array('code'=>1,'msg'=>'删除成功'));
        }catch(Exception $e){
            return json(array('code'=>0,'msg'=>$e->getMessage()));
        }
    }
    
    /**
     * 获取菜单
     */
    public function ajaxmenulist($pid = ''){
        $Menu = new \logic\sys\power\Menu($this->getUser());
        $list = $Menu->getList(['plat'=>$this->plat,'status'=>1]);
        $menulist = nodeMerge($list['list']);
        $menulist = $this->filterMenu($menulist);
        if( $pid ){
            foreach( $menulist as $val ){
                if( $val['id'] == $pid ){
                    $templist = $val['children'];
                    break;
                }
            }
            $menulist = $templist;
        }
        return json( array( 'code'=> 1,'msg'=>'请求成功','data'=>$menulist));
    }
    
    /**
     * 节点过滤
     */
    private function filterMenu($menulist){
        return $menulist;
    }
}